home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ GX 1.1.2 / Programming Stuff / Sample Code / General QDGX App Samples / QuickDraw GX Shell ƒ / put your code here.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  12.4 KB  |  423 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    put your code here.c
  5. |**|
  6. |**|    This file contains the calls that an application needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "color library.c", "font library.c", "graphics debug library.c",
  11. |**|    "shape library.c", and "transform library.c".
  12. |**|
  13. |**|    ©1992-1994  Apple Computer, Inc.
  14. |**|    All rights reserved.
  15. |**|
  16. |**| =====================================================================
  17. \**/
  18.  
  19.  
  20. #include "QDGX shell.h"
  21.  
  22.  
  23. /**\
  24. |**| ---------------------------------------------------------------------
  25. |**| PROTOTYPES
  26. |**| ---------------------------------------------------------------------
  27. \**/
  28.  
  29. // funtions required by shell
  30.  
  31. void    DoSetup            (void);
  32. void    DoDraw            (WindowPtr wind, Boolean updating);
  33. OSErr    DoCreateNew        (void);
  34. void    DoDispose        (WindowPtr wind);
  35. void    DoIdle            (WindowPtr wind);
  36. void    DoTeardown        (void);
  37. void    DoClick            (WindowPtr wind, Point p);
  38.  
  39. // private functions
  40.  
  41. OSErr    DoWindowInit        (WindowPtr wind);
  42. void    CreateSampleImage    (WindowPtr wind);
  43.  
  44.  
  45. /**\
  46. |**| ---------------------------------------------------------------------
  47. |**| ENUMS
  48. |**| ---------------------------------------------------------------------
  49. \**/
  50. enum { rWindResource = 128 };
  51.  
  52.  
  53. /**\
  54. |**| ---------------------------------------------------------------------
  55. |**| GLOBALS
  56. |**| ---------------------------------------------------------------------
  57. \**/
  58. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  59. // functionality will only work with the "debugging" version of QuickDraw GX.
  60. // If the debugging version is not installed, nothing bad will happen, but these
  61. // functions will not work. 
  62.  
  63. Boolean        gDebugging = true;
  64.  
  65. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  66.  
  67. Boolean        gGiveMeValidation = true;
  68.  
  69.  
  70. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  71. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  72. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  73. // since we need abunch if we have several windows open.
  74.  
  75. long        gGraphicsHeapSize = 600;
  76.  
  77. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  78. // override.  This is so that our override can be native PowerPC code if necessary.
  79.  
  80. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  81.  
  82.  
  83.  
  84. /**\
  85. |**| ---------------------------------------------------------------------
  86. |**| DoSetup()
  87. |**| Here's where we initialize any global variables our application needs.
  88. |**| We have only one at this time -- the universal proc pointer for
  89. |**| our printing override.
  90. |**| ---------------------------------------------------------------------
  91. \**/
  92. void DoSetup (void)
  93. {    // Initialize our printing event override UPP
  94.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  95. }
  96.  
  97.  
  98. /**\
  99. |**| ---------------------------------------------------------------------
  100. |**| DoDraw()
  101. |**| Draw the contents of the window.  The first parameter is the window
  102. |**| to draw, and the second parameter is true if we're updating an existing
  103. |**| image.  If that's the case, we don't want to change anything, but
  104. |**| just draw what's already there.
  105. |**| ---------------------------------------------------------------------
  106. \**/
  107. void DoDraw (WindowPtr wind, Boolean updating)
  108. {
  109.      #pragma unused (updating)
  110.      GXDrawShape (GetDocShape(wind));
  111. }
  112.  
  113.  
  114. /**\
  115. |**| ---------------------------------------------------------------------
  116. |**| DoCreateNew()
  117. |**| This routine is called when a window needs to be created.
  118. |**| ---------------------------------------------------------------------
  119. \**/
  120. OSErr DoCreateNew (void)
  121. {
  122.     OSErr        err = noErr;
  123.     WindowPtr    wind;
  124.     
  125. // Get and create our window from the resource fork
  126.  
  127.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  128.  
  129. // Attach a default gxViewPort to it, create and iInitialize our
  130. // private data for it, and add a sample image to its page shape.
  131.  
  132.     if ( wind == NULL )
  133.         return (MemError());
  134.  
  135.     GXIgnoreGraphicsNotice(transform_already_set);
  136.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  137.     GXPopGraphicsNotice();
  138.     
  139.     err = DoWindowInit(wind);
  140.     if ( err != noErr )
  141.         return err;
  142.     
  143.     CreateSampleImage(wind);
  144.     return err;
  145. }
  146.  
  147.  
  148. /**\
  149. |**| ---------------------------------------------------------------------
  150. |**| DoDispose()
  151. |**| This routine is called when a window needs to be disposed of.
  152. |**| ---------------------------------------------------------------------
  153. \**/
  154. void DoDispose (WindowPtr wind)
  155. {
  156.     TH_Doc    doc;
  157.     
  158. // You should always dispose of your GX graphics objects before tossing your window.
  159. // Why?  It's generally good form and this approach guarantees that everything is
  160. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  161. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  162. // with notices set, you will receive a notice that you had not disposed of everything.
  163. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  164.     
  165.     if ( wind != NULL )
  166.     {
  167.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  168.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  169.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  170.         DisposHandle((Handle) doc);            // Dispose of our private data.
  171.         DisposeWindow(wind);                // Dispose of the window.
  172.     }
  173. }
  174.  
  175.  
  176. /**\
  177. |**| ---------------------------------------------------------------------
  178. |**| DoIdle()
  179. |**| This routine is called to do things while idling through the event loop.
  180. |**| ---------------------------------------------------------------------
  181. \**/
  182. void DoIdle (WindowPtr wind)
  183. {
  184. }
  185.  
  186.  
  187. /**\
  188. |**| ---------------------------------------------------------------------
  189. |**| DoTeardown()
  190. |**| This routine is called just before we quit to remove anything 
  191. |**| persistent that might have been setup by DoSetup().
  192. |**| ---------------------------------------------------------------------
  193. \**/
  194. void DoTeardown (void)
  195. {
  196.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  197. }
  198.  
  199.  
  200. /**\
  201. |**| ---------------------------------------------------------------------
  202. |**| DoClick()
  203. |**| ---------------------------------------------------------------------
  204. \**/
  205. void DoClick(WindowPtr window, Point p)
  206. {
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /**\
  214. |**| ---------------------------------------------------------------------
  215. |**| DoWindowInit()
  216. |**| In this function we create and initialize the the private document
  217. |**| structure for a new window.  This structure contains the print job and
  218. |**| the shape which is drawn in the window.  We store this data in a handle
  219. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  220. |**| this, rather than using globals, we can create many windows containing
  221. |**| unique print jobs and shapes.
  222. |**| ---------------------------------------------------------------------
  223. \**/
  224. OSErr DoWindowInit (WindowPtr wind)
  225. {
  226.     OSErr    err = noErr;
  227.     gxJob    docJob;
  228.     gxShape    docPage;
  229.     TH_Doc    windDoc;
  230.  
  231.  
  232. // Create the page shape. We set the unique items attribute to make sure that each item
  233. // added to the picture has a unique reference. If this attribute was not set, we would
  234. // not see all copies of anything we add to the shape multiple times -- we'd just see
  235. // the last version added.        
  236.  
  237.     docPage = GXNewShape(gxPictureType);
  238.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  239.     
  240.     
  241. // Create a print job for this document.  This will be the same as the system default until
  242. // the user goes through the dialogs for Page Setup or Print…
  243.  
  244.     err = GXNewJob(&docJob);
  245.     
  246.     
  247. // If there are no errors, create a handle the size of our document structure and store
  248. // the print job and page shape in it.  Store the handle in the window's refCon field so
  249. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  250. // can be used to do this easily.
  251.  
  252.     if ( err == noErr )
  253.     {
  254.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  255.  
  256.         if ( windDoc == NULL )
  257.             err = MemError();
  258.         else
  259.         {
  260.             (*windDoc)->docJob = docJob;
  261.             (*windDoc)->docPage = docPage;
  262.             SetWRefCon(wind, (long) windDoc);
  263.         }
  264.  
  265. // Now install our application override for PrintingEvent so that we can
  266. // support the new movable-modal printing dialog boxes.
  267.  
  268.         GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
  269.  
  270.     }
  271.  
  272.     return err;
  273. }
  274.  
  275.  
  276. /**\
  277. |**| ---------------------------------------------------------------------
  278. |**| CreateSampleImage()
  279. |**| This function creates primitive shapes and adds them to the window's page shape.
  280. |**| ---------------------------------------------------------------------
  281. \**/
  282. void CreateSampleImage (WindowPtr wind)
  283. {
  284.     gxShape            thePage;
  285.     gxShape         theLine;
  286.     gxLine            lineData = {{ff(25), ff(25)}, {ff(125), ff(125)}};
  287.     gxShape            theRect;
  288.     gxRectangle     rectData = {ff(25), ff(25), ff(75), ff(75)};    
  289.     gxShape         theCurve;
  290.     gxCurve         curveData = {{ff(25), ff(25)}, {ff(275), ff(75)}, {ff(125), ff(125)}};    
  291.     gxShape         thePath;
  292.     long             tripleEightData[] = {    1 /* # of contours */, 
  293.                                             6 /* # of points */, 
  294.                                             0xff000000,
  295.                                                0, 0,  // the points 
  296.                                                ff(75),  0, 
  297.                                                ff(5), ff(50),
  298.                                                ff(75),  ff(100),
  299.                                                0,  ff(100), 
  300.                                                ff(75), ff(50)};        
  301.     gxShape         theText;
  302.     gxRectangle     theTextBounds;
  303.     gxColor         textColor;
  304.     fixed            x,y;
  305.     short            loop;
  306.     gxShape         thePolygon;
  307.     long starData[] = {    1,                                  // number of contours
  308.                          5 ,                                 // number of points
  309.                         ff(60), 0, ff(90), ff(90),  ff(0), ff(30),
  310.                         ff(120), ff(30), ff(0), ff(90)};     // the points
  311.  
  312.  
  313. // Retrieve the page shape so we can add to it.
  314.  
  315.     thePage = GetDocShape(wind);
  316.     
  317.     
  318. // Create a line
  319.     
  320.     theLine = GXNewLine (&lineData);
  321.  
  322.     AddToShape(thePage, theLine);
  323.     GXDisposeShape(theLine);  
  324.  
  325.  
  326. // Create a rectangle which is: red & is draw with it's frame.
  327.  
  328.     theRect = GXNewRectangle(&rectData); 
  329.      SetShapeCommonColor (theRect, red);
  330.     GXSetShapeFill (theRect, gxClosedFrameFill);
  331.     GXMoveShapeTo (theRect,  ff(150), ff(25));
  332.  
  333.     AddToShape(thePage, theRect);
  334.     GXDisposeShape(theRect);  
  335.  
  336.  
  337. // Create a curve which has: a 3.25 pen thickness
  338.     
  339.     theCurve = GXNewCurve(&curveData); 
  340.     
  341.     // The fl marco converts floating gxPoint #'s to fixed gxPoint.
  342.     GXSetShapePen(theCurve, fl(3.25));
  343.     GXMoveShapeTo (theCurve,  ff(210), ff(25));
  344.  
  345.     AddToShape(thePage, theCurve);
  346.     GXDisposeShape(theCurve);  
  347.  
  348.  
  349. // Create apath which has: a 2 pen thickness, its color is green, and its drawn with its frame 
  350.                                                                
  351.     thePath = GXNewPaths((gxPaths *) tripleEightData);
  352.     GXSetShapeFill (thePath, gxClosedFrameFill);
  353.      GXSetShapePen(thePath, ff(2));
  354.     SetShapeCommonColor (thePath, green);
  355.  
  356.     GXMoveShapeTo (thePath,  ff(390), ff(25));
  357.  
  358.     AddToShape(thePage, thePath);
  359.     GXDisposeShape(thePath);  
  360.  
  361.  
  362. // Create a character S which is: colored in hsv space and it is rotated 15 degrees - six times
  363. // via the left bottom corner.  Create the text, set the font size, and set the font name
  364.  
  365.     theText = GXNewText(1,(unsigned char*)"S",  nil);
  366.     SetShapeCommonFont(theText, timesFont);
  367.     GXSetShapeTextSize(theText, ff(200));
  368.     GXMoveShapeTo (theText,  ff(25), ff(275));
  369.     GXSetShapeAttributes (theText,  (GXGetShapeAttributes(theText) | gxMapTransformShape));
  370.     
  371.  
  372. // Create an hsv color space and set up the initial colors
  373.  
  374.     textColor.space = gxHSVSpace;
  375.     textColor.profile = nil;
  376.     textColor.element.hsv.hue = 0x7400;
  377.     textColor.element.hsv.saturation = 0xFFFF;
  378.     textColor.element.hsv.value = 0xFFFF;
  379.  
  380.  
  381. // Get the bounds of "theText" and determine the bottom left corner
  382.  
  383.     GXGetShapeBounds(theText, 0L, &theTextBounds);
  384.     x = theTextBounds.left;
  385.     y = theTextBounds.bottom;
  386.  
  387.  
  388. // Rotate "theText" 15 degrees - 6 times. Add each letter to the picture.
  389.  
  390.     for (loop = 0; loop < 6; loop++) {
  391.         GXSetShapeColor(theText, &textColor);
  392.         GXRotateShape(theText, ff(15), x, y);
  393.     
  394.         AddToShape(thePage, theText);
  395.  
  396.         textColor.element.hsv.hue += 0x0940;
  397.     }
  398.     
  399.     GXDisposeShape(theText);  
  400.     
  401.                 
  402. // Create a polygon which has the following features: yellow, drawn with a pen = 3, and
  403. // skew it in the vertical direction by 0.5 
  404.  
  405.     thePolygon = GXNewPolygons((gxPolygons *) starData);
  406.     GXSetShapeFill(thePolygon, gxEvenOddFill);
  407.     GXSetShapePen (thePolygon, ff(3));
  408.     SetShapeCommonColor (thePolygon, yellow);
  409.     GXMoveShapeTo (thePolygon,  ff(240), ff(110));
  410.     GXSkewShape(thePolygon, 0, fl(0.5), 0, 0);
  411.     
  412.     AddToShape(thePage, thePolygon);
  413.     GXDisposeShape(thePolygon);  
  414.  
  415.  
  416. // Invalidate the window's portRect so that everything gets updated.
  417.     
  418.     SetPort(wind);
  419.     InvalRect(&wind->portRect);
  420. }
  421.  
  422.  
  423.